# encoding: utf-8
from pyo import *


class Grain:
    def __init__(self):
        
        self.table = SndTable(SNDS_PATH+"/transparent.aif")
        self.sndDur = self.table.getDur()

        self.env = HannTable()
        
        self.x = SigTo(value=0.5, time=0.05, init=0.5)
        self.y = SigTo(value=0.5, time=0.05, init=0.5)
        
        self.lfo = LFO(self.table.getRate()*self.x, type=3, mul=self.table.getSize(), add=Noise(10))

        self.z = Sig(0.001)  # Correspond au parametre 'z' de l'interface tactile

        self.pos = Port(self.lfo, risetime=.05, falltime=.05, mul=self.table.getSize())

        self.dur = Noise(mul=Clip(self.z, min=0, max=0.025), add=.1)

        self.grn = Granulator( table=self.table,    
                            env=self.env,        
                            pitch=self.y,      
                            pos=self.lfo,        
                            dur=self.dur,        
                            grains=40,      
                            basedur=.1,      
                            mul=.5)
                            
    def changeTable(self, snd):
        self.table.sound = snd
    
    def changeX(self, x):
        self.x.value = x*.5

    def changeY(self, x):
        self.y.value = x
    
    def grainDur(self,x):
        self.z.value = x

    def sig(self):
        return self.grn
        
class CrossSynth:
    def __init__(self,input):
        
        self.x = SigTo(value=0.5, time=0.05, init=0.5)
        self.y = SigTo(value=0.5, time=0.05, init=0.5)
        self.z = Sig(0.5)  
        self.snd1 = input
        
        self.freqs = []
        self.amps = []

        for i in range (50,500,50):
            self.freqs.append(i *2.50)
            self.amps.append(10./i)
        
        self.freqs_2 = [i * 100 for i in range(40) if (i%2) == 1]
        
        self.freqs_3 = [random.triangular(630,640) for i in range(40)]    
        
        self.freqs_4 = [midiToHz(m) for m in [48,60,67,72,76,79,82,84,86,87,88,89,90,91]]
        
        self.freqs_5 = [midiToHz(m) for m in [26,38,45,50,57,62,66,69,72,74,76,77,78,79,80,81]]
        
        self.snd2 = FM(carrier=self.freqs_3*self.y, ratio=self.z, index=self.x*40, mul=self.amps)
        
        self.fin1 = FFT(self.snd1, size=1024, overlaps=4)
        self.fin2 = FFT(self.snd2, size=1024, overlaps=4)
        # get magnitudes and phases of input sounds
        self.pol1 = CarToPol(self.fin1["real"], self.fin1["imag"])
        self.pol2 = CarToPol(self.fin2["real"], self.fin2["imag"])
        # times magnitudes and adds phases
        self.mag = self.pol1["mag"] * self.pol2["mag"] * 50
        self.pha = self.pol1["ang"] + self.pol2["ang"]
        # converts back to rectangular
        self.car = PolToCar(self.mag, self.pha)
        self.fout = IFFT(self.car["real"], self.car["imag"], size=1024, overlaps=4).mix(2).out()
        
    def changeX(self, x):
        self.x.value = x

    def changeY(self, x):
        self.y.value = x
        
    def setRatio(self,x):
        self.z.value = x
        

